package com.shipwe.loans.ui.presenter

import com.shipwe.loans.configs.ShipweConfigs
import com.shipwe.loans.http.ApiService
import com.shipwe.loans.http.ApiService.addCommonParams
import com.shipwe.loans.http.IHttpCallback
import com.shipwe.loans.pro.ShipwePresenter
import com.shipwe.loans.ui.view.IApplyView
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/25 13:45
 * @Annotation：贷款申请 Presenter 实现
 */
class ApplyPresenter(private val iView: IApplyView) : ShipwePresenter(iView) {

    /**
     * 获取贷款信息
     */
    fun loadApplyDetailsAction(){
        val params = JSONObject().addCommonParams()
        params.put("path", ApiService.API_PATH.SHIPWE_API_LOAN_APPLY_CUSTOM_LIMIT_PATH)
        params.put("product",iView.getApplyProductName())
        params.put("token",iView.getApplyProductToken())
        params.put("amount",iView.getApplyAmount())
        if (ShipweConfigs.isSwahili()){
            params.put("localLanguage","YES")
        }
        val applyDays = iView.getApplyDuration()
        if (applyDays.isNotEmpty()){
            params.put("days",applyDays)
        }
        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //是否允许修改申请金额，不允许修改则不显示Seekbar
                    val chooseAmountFlag = obj.optBoolean("chooseAmountFlag",false)
                    when(chooseAmountFlag){
                        true -> {//允许选择贷款额度
                            iView.showApplyOptionalViews(obj)
                        }
                        else -> {//不允许选择贷款额度
                            iView.showApplyNotOptionalViews()
                        }
                    }
                    //设置贷款期限
                    iView.showApplyDurationList(obj)
                    //设置明细
                    iView.showApplyDetailsList(obj)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }

    /**
     * 创建贷款申请订单
     */
    fun submitApplyAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.SHIPWE_API_LOAN_APPLY_ORDER_CREATE_PATH)
            put("product",iView.getApplyProductName())
            put("token",iView.getApplyProductToken())
            put("amount",iView.getApplyAmount())
            put("days",iView.getApplyDuration())
        }
        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.showApplySuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }
}